package com.infolab.aims.domain.workorder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;

import com.infolab.aims.common.SpecialToStringStyle;
import com.infolab.aims.common.bean.AbstractModelBean;
import com.infolab.aims.domain.Department;
import com.infolab.aims.domain.SystemUser;

@Entity
@Table(name="AMTB_WORKORDER")
//@NamedNativeQueries(
//		{
//			@NamedNativeQuery(query="select workOrderStatus,count(*) from AMTB_WORKORDER", name = "WorkOrder.getCountWorkOrderWeek")
//		}
//	)
public class WorkOrder extends AbstractModelBean implements Serializable {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer workOrderId;
	
	private String workOrderCode;
	
	private String workOrderStatus;
	
	@NotBlank(message="{error.empty}")
	private String workOrderType;
	
	private String workOrderRemarks;
	
	private Date timeCheckIn;
	private Date timeCheckOut;
	
	private Date dateBooking;
	
	@NotBlank(message="{error.empty}")
	private String vehicleId;
	
	@NotBlank(message="{error.empty}")
	private String driverNric;
	private String driverName;
	private String driverTelephoneNo;
	private String driverEmail;
	private String driverType;
	private String meVersion;
	private String companyCode;
	private String assetModelCode;
	
	@ManyToOne
	@JoinColumn(name="technicianUserName",referencedColumnName="userName")
	private SystemUser technician;
	
	@ManyToOne
	@JoinColumn(name="accessDepartmentId")
	private Department accessDepartment;
	
	@Transient
	private List<WorkOrderDetail> workOrderComplaints = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(WorkOrderDetail.class));
	
	@Transient
	private List<WorkOrderAdHoc> workOrderAdHocs = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(WorkOrderAdHoc.class));
	
	@Transient
	private List<WorkOrderDetail> workOrderRepairs = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(WorkOrderDetail.class));
	
	@Transient
	private List<WorkOrderPart> workOrderParts = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(WorkOrderPart.class));
	
	@Transient
	private List<WorkOrderTechnician> workOrderTehnicians = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(WorkOrderTechnician.class));

	public Integer getWorkOrderId() {
		return workOrderId;
	}

	public void setWorkOrderId(Integer workOrderId) {
		this.workOrderId = workOrderId;
	}

	public String getWorkOrderCode() {
		return workOrderCode;
	}

	public void setWorkOrderCode(String workOrderCode) {
		this.workOrderCode = workOrderCode;
	}

	public String getWorkOrderStatus() {
		return workOrderStatus;
	}

	public void setWorkOrderStatus(String workOrderStatus) {
		this.workOrderStatus = workOrderStatus;
	}

	public String getWorkOrderType() {
		return workOrderType;
	}

	public void setWorkOrderType(String workOrderType) {
		this.workOrderType = workOrderType;
	}

	public String getWorkOrderRemarks() {
		return workOrderRemarks;
	}

	public void setWorkOrderRemarks(String workOrderRemarks) {
		this.workOrderRemarks = workOrderRemarks;
	}

	public Date getTimeCheckIn() {
		return timeCheckIn;
	}

	public void setTimeCheckIn(Date timeCheckIn) {
		this.timeCheckIn = timeCheckIn;
	}

	public Date getTimeCheckOut() {
		return timeCheckOut;
	}

	public void setTimeCheckOut(Date timeCheckOut) {
		this.timeCheckOut = timeCheckOut;
	}

	public Date getDateBooking() {
		return dateBooking;
	}

	public void setDateBooking(Date dateBooking) {
		this.dateBooking = dateBooking;
	}

	public String getVehicleId() {
		return vehicleId;
	}

	public void setVehicleId(String vehicleId) {
		this.vehicleId = vehicleId;
	}

	public String getDriverNric() {
		return driverNric;
	}

	public void setDriverNric(String driverNric) {
		this.driverNric = driverNric;
	}

	public String getDriverName() {
		return driverName;
	}

	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}

	public String getDriverTelephoneNo() {
		return driverTelephoneNo;
	}

	public void setDriverTelephoneNo(String driverTelephoneNo) {
		this.driverTelephoneNo = driverTelephoneNo;
	}

	public String getDriverEmail() {
		return driverEmail;
	}

	public void setDriverEmail(String driverEmail) {
		this.driverEmail = driverEmail;
	}

	public String getDriverType() {
		return driverType;
	}

	public void setDriverType(String driverType) {
		this.driverType = driverType;
	}

	public String getMeVersion() {
		return meVersion;
	}

	public void setMeVersion(String meVersion) {
		this.meVersion = meVersion;
	}

	public SystemUser getTechnician() {
		return technician;
	}

	public void setTechnician(SystemUser technician) {
		this.technician = technician;
	}
	
	public Department getAccessDepartment() {
		return accessDepartment;
	}

	public void setAccessDepartment(Department accessDepartment) {
		this.accessDepartment = accessDepartment;
	}

	public List<WorkOrderDetail> getWorkOrderComplaints() {
		return workOrderComplaints;
	}

	public void setWorkOrderComplaints(List<WorkOrderDetail> workOrderComplaints) {
		this.workOrderComplaints = workOrderComplaints;
	}

	public List<WorkOrderAdHoc> getWorkOrderAdHocs() {
		return workOrderAdHocs;
	}

	public void setWorkOrderAdHocs(List<WorkOrderAdHoc> workOrderAdHocs) {
		this.workOrderAdHocs = workOrderAdHocs;
	}

	public List<WorkOrderDetail> getWorkOrderRepairs() {
		return workOrderRepairs;
	}

	public void setWorkOrderRepairs(List<WorkOrderDetail> workOrderRepairs) {
		this.workOrderRepairs = workOrderRepairs;
	}
	
	public List<WorkOrderPart> getWorkOrderParts() {
		return workOrderParts;
	}

	public void setWorkOrderParts(List<WorkOrderPart> workOrderParts) {
		this.workOrderParts = workOrderParts;
	}
	
	public List<WorkOrderTechnician> getWorkOrderTehnicians() {
		return workOrderTehnicians;
	}

	public void setWorkOrderTehnicians(List<WorkOrderTechnician> workOrderTehnicians) {
		this.workOrderTehnicians = workOrderTehnicians;
	}

	public boolean isWorkOrderStatusClosed()
	{
		return StringUtils.equalsIgnoreCase(WORKORDERSTATUS_CLOSED, workOrderStatus);
	}
	
	public boolean isWorkOrderStatusInProcess()
	{
		return StringUtils.equalsIgnoreCase(WORKORDERSTATUS_INPROCESS, workOrderStatus);
	}
	public boolean isWorkOrderStatusInQueue()
	{
		return StringUtils.equalsIgnoreCase(WORKORDERSTATUS_INQUEUE, workOrderStatus);
	}
	

	public String getCompanyCode() {
		return companyCode;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}
	
	public String getAssetModelCode() {
		return assetModelCode;
	}

	public void setAssetModelCode(String assetModelCode) {
		this.assetModelCode = assetModelCode;
	}

	public String getAuditContentHead()
	{
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
		.append("Work Order Code",workOrderCode).toString();
	}


	public static final String WORKORDERTYPE_OTHER="OTHER";
	public static final String WORKORDERTYPE_COMPLAINT="CMPLN";
	public static final String WORKORDERTYPE_WALKIN="WALKIN";
	
	public static final String WORKORDERSTATUS_INQUEUE="INQUEUE";
	public static final String WORKORDERSTATUS_INPROCESS="INPROCESS";
	public static final String WORKORDERSTATUS_COMPLETED="COMPLETED";
	public static final String WORKORDERSTATUS_CLOSED="CLOSED";
	public static final String WORKORDERSTATUS_CANCEL="CANCEL";
}
